package com.example.demo.controller;

import com.example.demo.common.domain.Employee;
import com.example.demo.common.domain.Leave_Record;
import com.example.demo.common.util.Result;
import com.example.demo.common.util.StringUtil;
import com.example.demo.service.DateService;
import com.example.demo.service.LeaveService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.net.ssl.HttpsURLConnection;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller

public class LeaveController {

    @Autowired
    @Qualifier("ds")
    DateService dateService;

    @Autowired
    @Qualifier("leaveServiceImpl")
    LeaveService leaveService;



    @RequestMapping("/leave")
    public String leave(HttpSession session, Model model){
        if(check(session))
           return "leave";
        else
            return "login";
    }

    @RequestMapping("/leave2")//搜索所有的请假表单，渲染到前端的列表中
    public String leave2(HttpSession session,Model model,
                         @RequestParam(required = false,defaultValue = "1") Integer pageNo,
                         @RequestParam(required = false,defaultValue = "6") Integer pageSize
    ){
        PageHelper.startPage(pageNo,pageSize);
        if(!check(session)){
            return "login";
        }
        List<Leave_Record> leave_records = leaveService.selectAll();
        PageInfo<Leave_Record> pageInfo = new PageInfo<>(leave_records);
        pageInfo.setPageSize(pageSize);
        model.addAttribute("pageInfo",pageInfo);
        return "leave2";
    }
    @RequestMapping("/leave3")//通过传入的请假号，将该请假表单渲染到前端
    public String leave3(HttpSession session,Model model,Integer lid){
        if(!check(session)){
            return  "login";
        }
        Leave_Record leave_record = leaveService.selectByPrimaryId(lid);
        model.addAttribute("leave",leave_record);
        String percent = "0";
        if(leave_record.getGeneralagree() == 1){
            percent = "100";
        }else if(leave_record.getDeputyagree() == 1){
            percent = "66";
        }else{
            percent = "33";
        }

        model.addAttribute("percent",percent);
        return "leave3";
    }


    public boolean check(HttpSession session){
        if(session.getAttribute("employee")!=null)
            return true;
        else
            return false;
    }
    @RequestMapping("/subLeave")
    @ResponseBody//提交请假，根据用户提交的数据产生一条新的请假记录，存入数据库中
    public Map subLeave(HttpSession session,String startDate,String startTime,
                        String endDate,String endTime,String reason,String option){
        Employee e = (Employee)session.getAttribute("employee");


        startDate = StringUtil.rmQuote(startDate);
        startTime = StringUtil.rmQuote(startTime);
        endDate = StringUtil.rmQuote(endDate);
        endTime = StringUtil.rmQuote(endTime);
        option = StringUtil.rmQuote(option);
        reason = StringUtil.rmQuote(reason);

        Date from = dateService.parseDate(startDate, startTime);
        Date to = dateService.parseDate(endDate,endTime);
//        String format = dateService.dateFormat(date);
        Leave_Record leave_record = new Leave_Record();

        leave_record.setLdescription(reason);
        leave_record.setLstarttime(from);
        leave_record.setLendtime(to);
        leave_record.setEid(e.getEid());
        leave_record.setLtype(option);
        leave_record.setGeneralagree(0);//总经理
        leave_record.setDeputyagree(0);//副总经理
        leave_record.setDividagree(0);//部门经理
        leaveService.insertSelective(leave_record);


        return Result.success(true);
    }


}
